icon Clean up your Web pages
with HTML TIDY

Copyright © 1998 W3C, see tidy.c for copyright notice.

With many thanks to Hewlett Packard for financial support during the development of this software!

This version 13th November 1998

To get the latest version of Tidy please visit the original version of this page at: http://www.w3.org/People/Raggett/tidy. Courtesy of Netmind, you can register for email reminders when new versions of tidy become available.

Introduction to TIDY

When editing HTML it's easy to make mistakes. Wouldn't it be nice if there was a simple way to fix these mistakes automatically and tidy up sloppy editing into nicely layed out markup? Well now there is! Dave Raggett's HTML TIDY is a free utility for doing just that. It also works great on the attrociously hard to read markup generated by specialized HTML editors and conversion tools, and can help you identify where you need to pay further attention on making your pages more accessible to people with disabilities.

Tidy is able to fix up a wide range of problems and to bring to your attention things that you need to work on yourself. Each item found is listed with the line number and column so that you can see where the problem lies in your markup. Tidy won't generate a cleaned up version when there are problems that it can't be sure of how to handle. These are logged as "errors" rather than "warnings".

Examples of TIDY at work

Tidy corrects the markup in a way that matches where possible the observed rendering in popular browsers from Netscape and Microsoft. Here are just a few examples of how TIDY perfects your HTML for you:

Layout style

You can choose which style you want Tidy to use when it generates the cleaned up markup: for instance whether you like elements to indent their contents or not.

Internationalization issues

Tidy offers you a choice of character encodings: US ASCII, ISO Latin-1, UTF-8 and the ISO 2022 family of 7 bit encodings. The full set of HTML 4.0 entities are defined. Cleaned up output uses HTML entity names for characters when appropriate. Otherwise characters outside the normal range are output as numeric character entities.

Accessibility

Tidy offers advice on accessibility problems for people using non-graphical browsers. The most common thing you will see is the suggestion you add a summary attribute to table elements. The idea is to provide a summary of the table's role and structure suitable for use with aural browsers.

Cleaning up presentational markup

Many tools generate HTML with an excess of FONT, NOBR and CENTER tags. Tidy's -clean option will replace them by style properties and rules using CSS. This makes the markup easier to read and maintain as well as reducing the file size! Tidy is expected to get smarter at this in the future.

Support for XML

XML processors compliant with W3C's XML 1.0 recommendation are very picky about which files they will accept. Tidy can help you to fix errors that cause your XML files to be rejected. Tidy doesn't yet recognize all XML features though, e.g. it doesn't yet understand CDATA sections or DTD subsets.

Creating Slides

The -slides option allows you to burst a single HTML file into a number of linked slides. Each H1 element in the input file is treated as delimiting the start of a slide. The slides are named slide1.html, slide2.html, slide3.html etc. This is a new feature and ideas are welcomed as to how to improve it.

Indenting text for a better layout

 <html>
   <head>
   </head>
   <body>
     <p>
       para which has enough text to cause a line break, and so test
       the wrapping mechanism for long lines.
     </p>
 <pre>This is
 <em>genuine
       preformatted</em>
    text
 </pre>
     <ul>
       <li>
         1st list item
       </li>
       <li>
         2nd list item
       </li>
     </ul>
     <!-- end comment -->
   </body>
 </html>

and this is the default style:

 <html>
 <head>
 </head>
 <body>
 <p>para which has enough text to cause a line break, and so test
 the wrapping mechanism for long lines.</p>

 <pre>This is
 <em>genuine
       preformatted</em>
    text
 </pre>

 <ul>
 <li>1st list item </li>

 <li>2nd list item</li>
 </ul>

 <!-- end comment -->
 </body>
 </html>

How to run tidy

   tidy [[options] filename]*

HTML tidy is not (yet) a windows program. If you run tidy without any arguments, it will just sit there waiting to read markup on the stdin stream. Tidy's input and output default to stdin and stdout respectively. Errors are written to stderr but can be redirected to a file with the -f filename option.

I generally use the -m option to get tidy to update the original file, and if the file is particularly bad I also use the -f option to write the errors to a file to make it easier to review them. Tidy supports a small set of character encoding options. The default is ASCII, which makes it easy to edit markup in regular text editors.

For instance:

   tidy -f errs.txt -m index.html

which runs tidy on the file "index.html" updating it in place and writing the error messages to the file "errs.txt". Its a good idea to save your work before tidying it, as with all complex software, tidy may have bugs. If you find any please let me know!

To get a list of available options use:

   tidy -help

You should see something like this:

   options for tidy vers: 11th June 1998

   -indent or -i       indent element content
   -omit or -o         omit optional endtags
   -wrap 72         wrap text at column 72 (default is 68)
   -upper or -u        force tags to upper case
   -clean or -c        replace font, nobr & center tags by CSS
   -raw                don't o/p entities for chars 128 to 255
   -ascii              use ASCII for output, Latin-1 for input
   -latin1             use Latin-1 for both input and output
   -utf8               use UTF-8 for both input and output
   -iso2022            use ISO2022 for both input and output
   -modify or -m       to modify original files
   -errors or -e       show only error messages
   -f file             write errors to file
   -xml                use this when input is in XML
   -asxml              to convert HTML to XML
   -slides             to burst into slides on h1 elements
   -help               list command line options

Input and Output default to stdin/stdout respectively. Single letter options apart from -f may be combined as in: tidy -f errs.txt -imu foo.html

Recent Changes

Sorry if I haven't dealt with all the feedback in this release, I hope to do a major new release in December '98.

Fixed bug wherein <style type=text/css> was written out as <style type="text/ss">.

Tidy now handles wrapping of attributes containing JavaScript text strings, inserting the line continuation marker as needed, for instance:

onmouseover="window.status='Mission Statement, \
Our goals and why they matter.'; return true"

You can now set the wrap margin with the -wrap option.

When the output is XML, tidy now ensures the content starts with <?xml version="1.0"?> as required by the W3C Recommendation for XML.

The Document type for HTML 2.0 is now "-//IETF//DTD HTML 2.0//". In previous versions of tidy, it was incorrectly set to "-//W3C//DTD HTML 2.0//".

When using the -clean option isolated FONT elements are now mapped to SPAN elements. Previously these FONT elements were simply dropped.

NOFRAMES now works fine with BODY element in frameset documents.

Future releases may address:

Implementation details

The code is in ANSI C and uses the C standard library for i/o. The parser is thread-safe although the code for pretty printing the parse tree is not (yet). The parser works top down, building a complete parse tree in memory. Document text is held as Unicode represented as UTF-8 in a character buffer that expands as needed. The code has so far been tested on Windows'95, Windows NT, Linux, MacOS, BeOS, SunOS, Solaris, IRIX and HP-UX, amongst others.

tidy13nov98.tgz
gzipped tar file for source code
tidy.exe
Windows 95/NT executable (32-bit Windows console-mode program)
tidy_03aug98.ppc.zip
Zipped BeOS Power PC executable (3rd August 1998 version)
tidy.lha
Amiga executable (1st September 1998 version)
platform.h, html.h
the include files with common definitions
lexer.c
lexical analysis and buffer management
parser.c
HTML and XML parsers
tags.c
dictionary of tags and their properties
attrs.c
dictionary of attributes and their properties
istack.c
stack of active inline elements
entities.c
dictionary of entities
clean.c
smarts for cleaning up presentational markup
pprint.c
pretty printing for HTML and XML
localize.c
Change this file to localize tidy's messages
tidy.c
main() and error reporting routines
Makefile
Makefile for gcc

To compile Tidy using gcc, you can use the makefile or simply type:

   gcc -o tidy *.c

Dave Raggett <dsr@w3.org> is an engineer from Hewlett Packard's UK Laboratories, and works on assignment to the World Wide Web Consortium, where he is the W3C lead for HTML.